package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/9/3
 * Time: 11:11
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据！");

        List<Dept> deptList =  deptService.list();
        return Result.success(deptList);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除数据" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Dept dept) {
        log.info("新增部门名称");
        deptService.insertByDeptName(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findByDeptId(@PathVariable Integer id) {
        log.info("根据部门id查询数据");
        Dept dept =  deptService.findById(id);
        return Result.success(dept);
    }


    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门名称");
        deptService.updateDeptName(dept);
        return Result.success();
    }

}
